package com.poetize.plus.controller.admin;

import com.poetize.plus.entity.dto.MenuDTO;
import com.poetize.plus.entity.dto.MenuTreeDTO;
import com.poetize.plus.entity.vo.MenuTreeVO;
import com.poetize.plus.entity.vo.MenuVO;
import com.poetize.plus.service.MenuService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zuosy
 * @created 2024/3/29 9:18
 */
@Tag(name = "菜单信息")
@RestController
@RequestMapping("/admin/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    @GetMapping("/menuTree")
    @Operation(summary = "菜单树数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<List<MenuTreeVO>> menuTree(@Validated MenuTreeDTO menuTreeDTO){
        return R.ok(menuService.menuTree(menuTreeDTO));
    }

    @PostMapping("/addMenu")
    @Operation(summary = "新增菜单信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> addMenu(@RequestBody @Validated (Insert.class) MenuDTO menuDTO){
        return R.ok(menuService.addMenu(menuDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "菜单信息详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<MenuVO> queryMenu(@PathVariable("id") Long id){
        return R.ok(menuService.queryMenu(id));
    }

    @PostMapping("/updateMenu")
    @Operation(summary = "修改菜单信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateMenu(@RequestBody @Validated(Update.class) MenuDTO menuDTO){
        return R.ok(menuService.updateMenu(menuDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteMenu(@PathVariable("id") Long id){
        return R.ok(menuService.deleteMenu(id));
    }
}
